﻿Imports CObject
Imports ConnectionServer
Imports System.Data.SqlClient

Namespace CMT

    Public Class Questions
        Inherits CObject.CObject

        Public Overrides Function Clone() As CObject.CObject
            Return New Questions
        End Function

        Public Overrides ReadOnly Property Key As String
            Get
                Return "Id"
            End Get
        End Property

        Protected Overrides Function ProcedureSigniture(ByVal peCommandType As CObject.CommandType) As String
            Select Case peCommandType
                Case CommandType.SQLSelect
                    Return "EXEC CMT_GetRegQuestForEvent {EventId}"
                Case Else
                    Throw New SystemException("Invalid Command Type")
            End Select
        End Function
        ''' <summary>
        ''' Method to get the Question details.
        ''' </summary>
        ''' <returns></returns>
        ''' <remarks></remarks>
        Public Function GetQuestionInfo() As DataTable
            Using loConn As Connection = New Connection(Connection.SourceDataBase.CMT)
                Dim lsSQl As String = String.Empty
                Dim loDT As DataTable
                lsSQl = "SELECT [Name] FROM [CMT_Reg_Questions] WHERE Isactive = 1"
                loDT = loConn.GetDataTable(lsSQl)
                Return loDT
            End Using
        End Function

        Public Overrides ReadOnly Property SourceDataBase As ConnectionServer.Connection.SourceDataBase
            Get
                Return Connection.SourceDataBase.CMT
            End Get
        End Property

#Region "properties"

        Public ReadOnly Property Id As Integer
            Get
                Return CastToInteger(GetProperty("Id"))
            End Get
        End Property

        Public Property EventId As Integer
            Get
                Return CastToInteger(GetProperty("EventId"))
            End Get
            Set(ByVal value As Integer)
                SetProperty("EventId", value)
            End Set
        End Property

        Public Property Name As String
            Get
                Return CastToString(GetProperty("Name"))
            End Get
            Set(ByVal value As String)
                SetProperty("Name", value)
            End Set
        End Property

        Public Property Code As String
            Get
                Return CastToString(GetProperty("Code"))
            End Get
            Set(ByVal value As String)
                SetProperty("Code", value)
            End Set
        End Property

        Public Property AnswerType As String
            Get
                Return CastToString(GetProperty("AnswerType"))
            End Get
            Set(ByVal value As String)
                SetProperty("AnswerType", value)
            End Set
        End Property
#End Region

#Region "Methods"
        ''' <summary>
        ''' Assign/remove questions against an event
        ''' </summary>
        ''' <param name="eventId"></param>
        ''' <param name="questXml"></param>
        ''' <param name="createdBy"></param>
        ''' <returns>rows affected</returns>
        ''' <remarks></remarks>
        Public Function MapQuestionsToEvent(ByVal eventId As Integer, ByVal questXml As String, ByVal createdBy As String) As Integer
            Dim rowsAffected As Integer = 0
            Using loConn As Connection = New Connection(SourceDataBase)

                Dim loCommand As SqlCommand = New SqlCommand
                'set the connection
                loCommand.Connection = loConn.Connection

                ' Set the parameters
                loCommand.Parameters.Add("@pEventId", SqlDbType.Int).Value = eventId
                loCommand.Parameters.Add("@pQustXML", SqlDbType.NVarChar).Value = questXml
                loCommand.Parameters.Add("@pCreatedBy", SqlDbType.NVarChar, 100).Value = createdBy

                loCommand.CommandType = Data.CommandType.StoredProcedure
                loCommand.CommandText = "CMT_MappRegQuestForEvent"

                'Execute the Query
                rowsAffected = loCommand.ExecuteNonQuery()
                loConn.Connection.Close()

            End Using
            Return rowsAffected
        End Function
#End Region

    End Class
End Namespace
